/*
 * Created on Jan 23, 2011 by Eng. Julian Rousselot
 *  
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package main;

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Hashtable;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.event.*;

public class MapScale extends JPanel {
    BufferedImage image;
    double scale = 1.0;

    public MapScale(BufferedImage image) {
        this.image = image;
    }

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g;
        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                            RenderingHints.VALUE_INTERPOLATION_BICUBIC);
        double x = (getWidth() - scale*image.getWidth())/2;
        double y = (getHeight() - scale*image.getHeight())/2;
        AffineTransform at = AffineTransform.getTranslateInstance(x,y);
        at.scale(scale, scale);
        g2.drawRenderedImage(image, at);
    }

    public Dimension getPreferredSize() {
        int w = (int)(scale*image.getWidth());
        int h = (int)(scale*image.getHeight());
        return new Dimension(w, h);
    }

    public JSlider getSlider() {
        //int min = 1, max = 36, inc = 5;
    	int min = 1, max = 4, inc = 1;
        final JSlider slider = new JSlider(min, max, 1);
        slider.setMajorTickSpacing(5);
        slider.setMinorTickSpacing(1);
        slider.setPaintTicks(true);
        slider.setSnapToTicks(true);
        slider.setLabelTable(getLabelTable(min, max, inc));
        slider.setPaintLabels(true);
        
        slider.addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent e) {
                int value = slider.getValue();
                System.out.println("Valor SLIDER: " + slider.getValue());
                //scale = (value+4)/20.0;
                scale = value;
                revalidate();
                repaint();
            }
        });
        return slider;
    }

    private Hashtable getLabelTable(int min, int max, int inc) {
        Hashtable<Integer,JLabel> table = new Hashtable<Integer,JLabel>();
        
        for(int j = min; j <= max; j += inc) {
            //String s = String.format("%.2f", (j+4)/20.0);
        	String s = String.format("%.2f", j*1.0);
            table.put(Integer.valueOf(j), new JLabel(s));
        }
        /* for(int j = min; j <= max; j += inc) {
            //String s = String.format("%.2f", (j+4)/20.0);
        	String s = String.format("%.2f", j/4.0);
            table.put(Integer.valueOf(j), new JLabel(s));
        }*/
       /* table.put(Integer.valueOf(1), new JLabel("0.50"));
        table.put(Integer.valueOf(2), new JLabel("1"));
        table.put(Integer.valueOf(3), new JLabel("1.50"));
        table.put(Integer.valueOf(4), new JLabel("2"));*/
        return table;
    }
    
    public double getScale() {
		return scale;
	}

	public void setScale(double scale) {
		this.scale = scale;
	}

	public BufferedImage getImage() {
		return image;
	}

	public void setImage(BufferedImage image) {
		this.image = image;
	}

	public static void main(String[] args) throws IOException {
        String path = "/home/julian/eclipseWorkspace/Visual-AORE/src/icons/document.jpg";
        BufferedImage image = ImageIO.read(new File(path));
        MapScale test = new MapScale(image);
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().add(new JScrollPane(test));
        f.getContentPane().add(test.getSlider(), "Last");
        f.setSize(400,400);
        f.setLocation(200,200);
        f.setVisible(true);
    }
}